/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.ifrn.Dao;

import br.edu.ifrn.Classes.Conexao;
import br.edu.ifrn.Classes.Disciplina;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MONTENEGRO
 */
public class DisciplinaDao {

    private Connection connection;

    public DisciplinaDao() {
        this.connection = new Conexao().getConnection();
    }

    public void InserirDisciplina(Disciplina disciplina) {
        String sql = "insert into Disciplina" + "(id, nome, cargahoraria, idcurso)"
                + "values(?,?,?,?)";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);

            stmt.setInt(1, disciplina.getIdDisciplina());
            stmt.setString(2, disciplina.getNomeDisciplina());
            stmt.setString(3, disciplina.getCargaHoraria());
            stmt.setInt(4, disciplina.getIdCurso());

            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void RemoverDisciplina(Disciplina disciplina) {
        try {
            PreparedStatement stmt = connection.prepareStatement("delete from Disciplina WHERE id=?");
            stmt.setInt(1, disciplina.getIdDisciplina());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void EditarDisciplina(Disciplina disciplina) {

        String sql = "update Disciplina set nome=?, cargahoraria=? where id=?";

        try {
            java.sql.PreparedStatement stmt = this.connection.prepareStatement(sql);

            stmt.setString(1, disciplina.getNomeDisciplina());
            stmt.setString(2, disciplina.getCargaHoraria());
            stmt.setInt(3, disciplina.getIdDisciplina());

            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public List<Disciplina> getLista() {

        try {
            List<Disciplina> disciplinas = new ArrayList<Disciplina>();
            PreparedStatement stmt = this.connection.prepareStatement("select * from Disciplina");
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {
                //criando o objeto disciplina
                Disciplina disciplina = new Disciplina();
                disciplina.setIdDisciplina(rs.getInt("id"));
                disciplina.setNomeDisciplina(rs.getString("nome"));
                disciplina.setCargaHoraria(rs.getString("cargahoraria"));

                //adicionando o objeto à lista
                disciplinas.add(disciplina);

            }
            stmt.execute();
            stmt.close();
            return disciplinas;

        } catch (SQLException e) {
            throw new RuntimeException(e);

        }

    }
}
